<?php
namespace Admin\Controller;
use Think\Controller;
use Lib\Cate\Level;
class BlogController extends CommonController{
	/**
	 * 博文列表
	 */
	public function index(){
		$search = I('get.search');
		$db = D('BlogView');
		$count = $db->where(array('title'=>array('like',"%".$search."%"),'del'=>0))->count();
		$pagesize = 10;
		$Page = new \Think\Page($count,$pagesize);
		$limit = $Page->firstRow.','.$Page->listRows;
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','第%TOTAL_PAGE%页');
		$Page->setConfig('header','<span class="rows">每页'.$pagesize.'条 共%TOTAL_PAGE%页,%TOTAL_ROW%条记录</span>');
		$Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		$this->blog = $db->getBlogs($limit,$search);
		$this->page  = $Page->show();
		$this->display();
	}
	/**
	 * 添加博文
	 */
	public function addBlog(){
		//读取栏目
		$cate = M('category')->order('sort')->select();
		$cate = Level::unlimitedForLevel($cate,'&nbsp;&nbsp----&nbsp;&nbsp;');
		$this->cate = $cate;
		//读取属性
		$attr = M('attr')->select();
		$this->attr = $attr;
		$this->display();
	}
	/**
	 * 编辑博文
	 */
	public function editBlog(){
		//读取栏目
		$cate = M('category')->order('sort')->select();
		$cate = Level::unlimitedForLevel($cate,'&nbsp;&nbsp----&nbsp;&nbsp;');
		$this->cate = $cate;
		//读取属性
		$attr = M('attr')->select();
		$this->attr = $attr;


		$id = I('get.id','','int');
		$this->blog = M('Blog')->where(array('id'=>$id))->find();
		$this->display();
	}
	/**
	 * 编辑博文表单处理
	 */
	public function runEditBlog(){
		if(!IS_POST)	E('页面不存在');
		$data = array(
			'id'		=>	I('post.id','','int'),
			'title'		=>	I('post.title'),
			'author'	=>	I('post.author'),
			'content'	=>	I('post.editorValue'),
			'tags'		=>	I('post.tags'),
			'click'		=>	I('post.click'),
			'cid'		=>	I('post.cid','','int'),
			'description'	=>	I('post.description'),
			'type'		=>	I('type',0,'int'),
			);
		$data['content'] = stripslashes($data['content']);
		if(M('blog')->save($data)){
			//取第一张图片为缩略图
			$content = $data['content'];
			//$strip = stripslashes($content);
			$url="/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/is";//正则匹配第一张图片
			if (preg_match_all($url, $content, $matches)) {
				$thumbUrl1 = pic_url($matches,$data['id']);
			}else{
				$thumbUrl1 = "/Uploads/default.jpg";
			}
			M('picture')->where(array('bid'=>$data['id']))->save(array('url'=>$thumbUrl1));
			$this->success('文章修改成功！',U('index'));
		}else{
			$this->error('文章修改失败!,请重试...');
		}
	}
	/**
	 * 添加博文表单处理
	 */
	public function runAddBlog(){
		if(!IS_POST)	E('页面不存在');
		$data = array(
			'title'		=>	I('post.title'),
			'author'	=>	I('post.author'),
			'content'	=>	I('post.editorValue'),
			'tags'		=>	I('post.tags'),
			'time'		=>	time(),
			'click'		=>	I('post.click'),
			'cid'		=>	I('post.cid','','int'),
			'description'	=>	I('post.description'),
			'type'		=>	I('type',0,'int'),
			);
		$data['content'] = stripslashes($data['content']);
		if($bid = M('blog')->add($data)){
			//取第一张图片为缩略图
			$content = $data['content'];

			//$strip = stripslashes($content);
			$url="/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/is";//正则匹配第一张图片
			if (preg_match_all($url, $content, $matches)) {
				$thumbUrl1 = pic_url($matches,$bid);
			}else{
				$thumbUrl1 = "/Uploads/default.jpg";
			}
			M('picture')->add(array('url'=>$thumbUrl1,'bid'=>$bid));
			$this->success('文章发布成功！',U('index'));
		}else{
			$this->error('文章发布失败!,请重试...');
		}
	}
	/**
	 * 回收站
	 */
	public function trach(){
		$search = I('get.search');

		$db = D('BlogView');
		$count = $db->where(array('del'=>1,'title'=>array('like',"%".$search."%")))->count();
		$pagesize = 10;
		$Page = new \Think\Page($count,$pagesize);
		$limit = $Page->firstRow.','.$Page->listRows;
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','第%TOTAL_PAGE%页');
		$Page->setConfig('header','<span class="rows">每页'.$pagesize.'条 共%TOTAL_PAGE%页,%TOTAL_ROW%条记录</span>');
		$Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		$this->blog = $db->getBlogs($limit,$search,1);
		$this->page  = $Page->show();
		$this->display('index');
	}
	/**
	 * 删除到回收站／还原
	 */
	public function toTrach(){
		$id = I('get.id','','int');
		$del = I('get.del','','int');
		$msg = $del ? '删除' : '还原';
		$update = array('id'=>$id,'del'=>$del);
		if(!M('blog')->save($update)){
			$this->error($msg.'失败，请重试..');
		}else{
			$this->success($msg.'成功',U('index'),1);
		}
	}
	/**
	 * 彻底删除
	 */
	public function delete(){
		$id = I('get.id','','int');
		if(M('Blog')->delete($id) && M('picture')->where(array('bid'=>$id))->delete()){
			@unlink('./Uploads/thumb/'.$id.'.jpg');
			$this->success('已经彻底删除这篇文章...',U('index'),1);
		}else{
			$this->error('删除失败，请重试..');
		}
	}
	/**
	 * 清空回收站
	 */
	public function deleteAll(){
		$id = M('blog')->where(array('del'=>1))->getField('id',true);

		if(M('Blog')->where(array('del'=>1))->delete() && M('picture')->where(array('bid'=>array('IN',$id)))->delete() ){
			foreach($id as $v){
				@unlink('./Uploads/thumb/'.$v.'.jpg');
			}
			$this->success('清空回收站成功...',U('index'),1);
		}else{
			$this->error('清空回收站失败，请重试..');
		}
	}
	/**
	 * ueditor调用
	 */
	public function ueditor(){
		$data = new \Org\Util\Ueditor('',C('WATER_ON'));

        echo $data->output();
	}

}